package ogmios.components.templatetags;
import java.io.*;
import ogmios.core.*;
/**
* A premade tag to use parts of the file in a template.
*/
public class FileTag implements TemplateTag
{
 private final String tag;
 private final int filePart;
 public static final int FILE_NAME=1;
 public static final int PATH_NAME = 0;
 public static final int FULL_NAME = 2;
 public static final int UNEXTENDED_FILE_NAME=3;
 /**
 * Creates a new FileTag using ""name"" as the tag, and returns the unextended file name
 */
 public FileTag()
 {
   this("{name}");
 }
 /**
 * Creates a new FileTag that returns the unextended file name
 */
 public FileTag(String tag)
 {
   this(tag, UNEXTENDED_FILE_NAME);
 }
  /**
  * Creates a new FileTag.
  *
  * @param     tag     The tag to use
  * @param     filePart     The part of the file we want
  */
  public FileTag(String tag, int filePart)
  {
   this.tag = tag;
   this.filePart = filePart;
  }
  public String getTag()
  {
   return tag;
  }
  public String getValue(File templateFile)
  {
    try
    {
     return
      filePart == FULL_NAME ? templateFile.getCanonicalPath() :
      filePart == PATH_NAME ? OgmiosUtilities.getParentDirectory(templateFile.getCanonicalPath()) : 
      filePart == UNEXTENDED_FILE_NAME ? 
      OgmiosUtilities.unextendedFileName(OgmiosUtilities.getFileName(templateFile.getCanonicalPath())) : 
      OgmiosUtilities.getFileName(templateFile.getCanonicalPath());
    }
    catch(IOException ioException)
    {
     return "";
    }
  }
}
